/**
* This file is part of OntoApplications (project linked with OntoCompoFramework)
*
* Copyright (C) 2011 -  Christian Brel
*
* @author   Main    Christian Brel
**/
package ontocompo.lib;

import java.awt.BasicStroke;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;

import javax.swing.border.AbstractBorder;

public class PartialBorder extends AbstractBorder {
	
	public enum Side {
		TOP, RIGHT, BOTTOM, LEFT;
	}
	
	private int getSideIndex(Side side)
	{
		switch (side)
		{
		case TOP: return 0;
		case RIGHT: return 1;
		case BOTTOM: return 2;
		case LEFT: return 3;
		default : return 3;
		}
	}
	
	// top, right, bottom, left
	boolean [] visibilities = { false, false, false, false };
	float [] widths = { 0, 0, 0, 0};
	
	int [] padding = {0, 0, 0, 0};
	
	public void setPadding(int p) { 
		for(int i = 0; i < padding.length; padding[i++] = p);
		}
	
	public void setPaddinf(Side side, int p)
	{
		padding[getSideIndex(side)] = p;
	}
	
	public void setSide(Side side, boolean visible, float width)
	{
		int i = getSideIndex(side);
		visibilities[i] = visible;
		widths[i] = width;
	}

	@Override
	public void paintBorder(Component c, Graphics g, int x, int y, int width, int height)
	{
		Graphics2D grph = (Graphics2D) g;
		
		if (visibilities[0]) { 
			grph.setStroke(new BasicStroke(widths[0]));
			grph.drawLine(x+padding[3], y, x+width-padding[1], y);
			}
		
		if (visibilities[1]) { 
			grph.setStroke(new BasicStroke(widths[1]));
			grph.drawLine(x+width, y+padding[0], x+width, y+height-padding[2]-1);
			}
		
		if (visibilities[2]) { 
			grph.setStroke(new BasicStroke(widths[2]));
			grph.drawLine(x+width-padding[1], y+height-1, x+padding[3], y+height-1 );
			}
		
		if (visibilities[3]) { 
			grph.setStroke(new BasicStroke(widths[3]));
			grph.drawLine(x, y+height-padding[2]-1, x, y+padding[0]);
			}
	}
	
	@Override
	public Insets getBorderInsets(Component c, Insets insets)
	{
		/* a changer : selon test, si float, alors entier au dessus... */
		if (visibilities[0]) { insets.top = (int) Math.ceil(widths[0]);	}
		if (visibilities[1]) { insets.right = (int) Math.ceil(widths[1]);	}
		if (visibilities[2]) { insets.bottom = (int)Math.ceil(widths[2]);	}
		if (visibilities[3]) { insets.left = (int) Math.ceil(widths[3]);	}
		
		
		return insets;
		
	}
	
}
